﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;

namespace SABI
{
    public partial class frm0126 : Form
    {

        private string _GroupID = "";
        string sql = "";
        DataTable dtListUser = new DataTable();
        DataTable dtListUserInGroup = new DataTable();

        public frm0126()
        {
            InitializeComponent();
        }

        public frm0126(string GroupID)
        {
            InitializeComponent();
            _GroupID = GroupID;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm0126_Load(object sender, EventArgs e)
        {

        }

        private void frm0126_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        void loadDATA()
        {
            sql = string.Format("SELECT * FROM tb0070 WHERE GroupID='{0}'", _GroupID);
            DataTable dt = FRM.GetDataTable(sql);
            if (dt.Rows.Count < 1) { return; }
            txtGroupID.Text = dt.Rows[0]["GroupID"].ToString();
            txtGroupName.Text = dt.Rows[0]["GroupName"].ToString();
            chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString() != "0" ? true : false;

            sql = string.Format("Select tb0071.UserID, tb0074.UserName From tb0071 Left Join tb0074 On tb0071.UserID = tb0074.UserID Where GroupID = '{0}' ORDER BY tb0074.UserName", _GroupID);
            dtListUserInGroup = FRM.GetDataTable(sql);
            lbxUserInGroup.DataSource = dtListUserInGroup;
            lbxUserInGroup.DisplayMember = "UserName";
            lbxUserInGroup.ValueMember = "UserID";

            sql = "Select UserID ,UserName  From tb0074 ";
            sql += string.Format("Where Disabled=0 And UserID Not In (Select UserID From tb0071 Where GroupID = '{0}') Order by UserName", _GroupID);
            dtListUser = FRM.GetDataTable(sql);
            lbxUser.DataSource = dtListUser;
            lbxUser.DisplayMember = "UserName";
            lbxUser.ValueMember = "UserID";
            
        }

        private void cmdToRight_Click(object sender, EventArgs e)
        {
            if(lbxUser.SelectedIndex>-1)
            {
                DataRowView drv = (DataRowView)lbxUser.SelectedItem;
                dtListUserInGroup.Rows.Add(drv.Row.ItemArray);
                dtListUser.Rows.Remove(drv.Row);
                SortTable();
            }
        }

        private void cmdToLeft_Click(object sender, EventArgs e)
        {
            if (lbxUserInGroup.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)lbxUserInGroup.SelectedItem;
                dtListUser.Rows.Add(drv.Row.ItemArray);
                dtListUserInGroup.Rows.Remove(drv.Row);
                SortTable();
            }
        }

        private void cmdToRightAll_Click(object sender, EventArgs e)
        {
            if(dtListUser.Rows.Count>0)
            {
                dtListUserInGroup.Merge(dtListUser);
                dtListUser.Rows.Clear();
                SortTable();
            }
        }

        private void cmdToLeftAll_Click(object sender, EventArgs e)
        {
            if (dtListUserInGroup.Rows.Count > 0)
            {
                dtListUser.Merge(dtListUserInGroup);
                dtListUserInGroup.Rows.Clear();
                SortTable();
            }
        }

        void SortTable()
        {
            dtListUser.DefaultView.Sort = "UserName ASC";
            dtListUserInGroup.DefaultView.Sort = "UserName ASC"; 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sql = string.Format("Delete tb0071 Where GroupID = '{0}'", _GroupID);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString, false);

            if(dtListUserInGroup.Rows.Count>0)
            {
                for(int i=0; i<dtListUserInGroup.Rows.Count;i++)
                {
                    sql = string.Format("Insert tb0071 (GroupID, UserID) Values ('{0}','{1}')", _GroupID, dtListUserInGroup.Rows[i]["UserID"].ToString());
                    DATA.ExecuteNonQuery(sql, FRM.ConnectString, false);
                }
            }

            DLL02.Info("Cập nhật dữ liệu thành công!");

        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            string _UserID = "", _UserName = "";
            if (lbxUser.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)lbxUser.SelectedItem;
                _UserID = drv.Row.ItemArray[0].ToString();
                _UserName = drv.Row.ItemArray[1].ToString();
            }                
            else if (lbxUserInGroup.SelectedIndex > -1)
            {
                DataRowView drv = (DataRowView)lbxUserInGroup.SelectedItem;
                _UserID = drv.Row.ItemArray[0].ToString();
                _UserName = drv.Row.ItemArray[1].ToString();
            }
            if (_UserID == "") { return; }
            Form frm = new frm0127(_UserID, _UserName);
            frm.ShowDialog();
        }

    }
}
